package handler

import (
	"github.com/gin-gonic/gin"
	"choujiang/box_server/service"
	"choujiang/common/consts"
	"choujiang/common/models"
	"choujiang/common/utils"
	"strconv"
)

type UserCenterInfo struct {
	UserId   int64  `json:"user_id"`
	Nickname string `json:"nickname"`
	Avatar   string `json:"avatar"`
	Coupon   int64  `json:"coupon"`
}

func GetUserInfo(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	userExtra := model.GetUserExtraByUid(user.Id)
	ret := make(map[string]interface{})
	ret["user_id"] = user.Id
	ret["nickname"] = user.NickName
	ret["head_img_url"] = user.HeadImgUrl
	ret["coupon"] = userExtra.Coupon

	utils.JSON(ret, c)
}

/**
 绑定渠道
 */
func UserBindChannel(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	channel := form.Get("channel")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	errType := service.UserBindChannel(user, channel)
	if errType != nil {
		utils.Error(errType, c)
		return
	}
	utils.JSON("ok", c)
}

/**
 @description 个人中心
 @return mixed
 @author guxiang
 @version v1 2018-11-30
 */
func UserCenter(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	userExtra := model.GetUserExtraByUid(user.Id)
	ret := make(map[string]interface{})
	userInfo := &UserCenterInfo{
		UserId:   user.Id,
		Nickname: user.NickName,
		Avatar:   user.HeadImgUrl,
		Coupon:   userExtra.Coupon,
	}
	ret["user_info"] = userInfo
	friends, counpon := service.GetUserCenterNum(user)
	ret["friends"] = friends
	ret["counpon"] = counpon

	utils.JSON(ret, c)
}

/**
 @description 收益明细
 @return mixed
 @author guxiang
 @version v1 2018-11-30
 */
func EarnDetail(c *gin.Context) {
	form := c.Request.Form
	pageNo := form.Get("pageNo")
	pageSize := form.Get("pageSize")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	intPageNo, _ := strconv.Atoi(pageNo)
	intPageSize, _ := strconv.Atoi(pageSize)
	ret := make(map[string]interface{})
	detail := service.GetEarnDetail(user, intPageNo, intPageSize)
	ret["detail"] = detail
	utils.JSON(ret, c)
}

/**
 @description 获取海报列表
 */
func GetPoster(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	posters := service.GetPosters()

	utils.JSON(posters, c)
}

/**
 @description 获取未体验过的游戏
 */
func GetUnusedGame(c *gin.Context) {
	form := c.Request.Form
	gameId := form.Get("gameId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	game := service.GetUnusedGame(user, gameId)
	utils.JSON(game, c)
}
